package com.farm.code.server;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.time.TimeTool;
import com.farm.code.dao.FarmCodeTypeDaoInter;
import com.farm.code.domain.FarmCodeType;
import com.farm.code.server.FarmCodeTypeManagerInter;
import com.farm.core.sql.query.DataQuery;

/**
 * 工程类型
 * 
 * @author MAC_wd
 */
public class FarmCodeTypeManagerImpl implements FarmCodeTypeManagerInter {
	private FarmCodeTypeDaoInter farmCodeTypeDao;
//	private static final Logger log = Logger
//			.getLogger(FarmCodeTypeManagerImpl.class);

	@Override
	public FarmCodeType insertFarmCodeTypeEntity(FarmCodeType entity,
			LoginUser user) {
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setCusername(user.getName());
		entity.setEuser(user.getId());
		entity.setEusername(user.getName());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		if (entity.getParentid() == null) {
			entity.setParentid("NONE");
		}
		entity.setTreecode("NONE");
		entity = farmCodeTypeDao.insertEntity(entity);
		if (entity.getParentid().equals("NONE")) {
			entity.setTreecode(entity.getId());
		} else {
			entity.setTreecode(farmCodeTypeDao.getEntity(entity.getParentid())
					.getTreecode()
					+ entity.getId());
		}
		farmCodeTypeDao.editEntity(entity);
		return entity;
	}

	@Override
	public FarmCodeType editFarmCodeTypeEntity(FarmCodeType entity,
			LoginUser user) {
		FarmCodeType entity2 = farmCodeTypeDao.getEntity(entity.getId());
		entity2.setEuser(user.getId());
		entity2.setEusername(user.getName());
		entity2.setEtime(TimeTool.getTimeDate14());
		//entity2.setPstate(entity.getPstate());
		entity2.setPcontent(entity.getPcontent());
		entity2.setSort(entity.getSort());
		//entity2.setParentid(entity.getParentid());
		entity2.setName(entity.getName());
		//entity2.setTreecode(entity.getTreecode());
		farmCodeTypeDao.editEntity(entity2);
		return entity2;
	}

	@Override
	public void deleteFarmCodeTypeEntity(String entity, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		farmCodeTypeDao.deleteEntity(farmCodeTypeDao.getEntity(entity));
	}

	@Override
	public FarmCodeType getFarmCodeTypeEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return farmCodeTypeDao.getEntity(id);
	}

	@Override
	public DataQuery createFarmCodeTypeSimpleQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		DataQuery dbQuery = DataQuery.init(query, "farm_code_type",
				"id,PSTATE,PCONTENT,SORT,PARENTID,NAME,TREECODE");
		return dbQuery;
	}

	// ----------------------------------------------------------------------------------
	public FarmCodeTypeDaoInter getfarmCodeTypeDao() {
		return farmCodeTypeDao;
	}

	public void setfarmCodeTypeDao(FarmCodeTypeDaoInter dao) {
		this.farmCodeTypeDao = dao;
	}

}
